package burubaka.controller.mnt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import net.arnx.jsonic.JSON;

import org.slim3.controller.Navigation;
import org.slim3.util.ApplicationMessage;
import org.slim3.util.BeanUtil;

import burubaka.BurubakaConstants;
import burubaka.bat.RacePayOffBat;
import burubaka.common.controller.Controller;
import burubaka.model.RaceInfo;
import burubaka.service.RaceInfoService;

import com.google.appengine.api.datastore.Key;

/**
 * @author tk
 * 
 */
public class RaceInfoController extends Controller {

	private static final Logger log = Logger.getLogger(RaceInfoController.class.getName());

	private RaceInfoService raceInfoService = new RaceInfoService();
	private RacePayOffBat racePayOffBat = new RacePayOffBat();

	@Override
	public Navigation init() throws Exception {
		List<RaceInfo> raceList = raceInfoService.findAll();
		requestScope("raceList", raceList);
		return forward("/mnt/raceInfo.jsp");
	}

	/**
	 * 検索
	 * 
	 * @return Navigation
	 * @throws Exception
	 */
	public Navigation search() throws Exception {

		Key key = asKey("raceKey");
		String jsonText = raceInfoService.findJsonByKey(key);
		log.info(jsonText);

		response.setContentType("application/json; charset=UTF-8");
		response.getWriter().write(jsonText);
		response.getWriter().flush();

		return null;
	}

	/**
	 * 登録
	 * 
	 * @return Navigation
	 * @throws Exception
	 */
	public Navigation regist() throws Exception {

		Key key = asKey("raceKey");

		Map<String, Object> map = JSON.decode((String) reqMap.get("raceInfo"), HashMap.class);
		log.info(map.toString());

		RaceInfo raceInfo = raceInfoService.findByKey(key);
		BeanUtil.copy(map, raceInfo);

		raceInfoService.regist(raceInfo);

		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put(BurubakaConstants.RESPONSEKEY_MESSAGE,
				ApplicationMessage.get(BurubakaConstants.MESSAGEKEY_REG_SUCCESS, null));
		setJson(JSON.encode(resMap, true));

		return null;
	}

	/**
	 * 削除
	 * 
	 * @return Navigation
	 * @throws Exception
	 */
	public Navigation delete() throws Exception {

		Key key = asKey("raceKey");
		RaceInfo raceInfo = raceInfoService.findByKey(key);
		raceInfoService.deleteAll(raceInfo);

		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put(BurubakaConstants.RESPONSEKEY_MESSAGE,
				ApplicationMessage.get(BurubakaConstants.MESSAGEKEY_DEL_SUCCESS, null));
		setJson(JSON.encode(resMap, true));

		return null;
	}

	/**
	 * 予想計算
	 * 
	 * @return Navigation
	 * @throws Exception
	 */
	public Navigation calcHandicap() throws Exception {

		Key key = asKey("raceKey");
		RaceInfo raceInfo = raceInfoService.findByKey(key);
		racePayOffBat.execute(raceInfo);

		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put(BurubakaConstants.RESPONSEKEY_MESSAGE,
				ApplicationMessage.get(BurubakaConstants.MESSAGEKEY_REG_SUCCESS, null));
		setJson(JSON.encode(resMap, true));

		return null;
	}
}
